#ifndef MAPWIDGET_H
#define MAPWIDGET_H
#include <QWidget>
#include <QDebug>
#include <QGraphicsItem>
#include <stdint.h>
#include <math.h>
#include <string>

#include "common_struct/common_struct.h"
#include "common_struct/FlightPathMassage.h"
#include "view.h"
#include "configwidget.h"
#include "cells.h"
#include "common_struct/safe_semaphore.h"


class MapWidget : public QWidget
{
    Q_OBJECT
public:
    MapWidget(QWidget* parent = 0);
    ~MapWidget();

public slots:
    int refresh_track(Targets_Info *data, int count);
    int refresh_target(Targets_Info *points, int count);

private slots:
    void RadarParameterChanged(dvec2, float, float, float, float, float);
    void refreshTrackersSlot();
    void refreshPointsSlot();
    void getScale(float scale);
    void clearMap();

signals:
    emittoPaintTracker();
    emittoPaintPoints();

private:
    //widgets
    View *view;
    GraphicsScene* scene;
    QHBoxLayout* mainlayout;
    RadarParamWidget* RadarParameterWidget;
    TrackerController *TrackSettingWidget;

    //functions
    void   setupMap(dvec2 LonLat, float distance);//设置地图
    void   setupRadarParameter(dvec2 Location, float DetectionDistance, float DirectionAngle, float StartAngle, float EndAngle, float northOffset);//设置雷达定位图标
    ulvec2 transferLonLattoTileMap_rtn_uint64_t(dvec2 LonLat, uint32_t zoom);
    dvec2  transferLonLattoTileMap_rtn_double(dvec2 LonLat, uint32_t zoom);
    dvec2  getPositionontheScene(float distance, float angle);
    dvec2  getRegionontheScene(dvec2 MapCentralPos, dvec2 RadarCentralPos, uint32_t zoom);
    int    calcNeededTilesNumbers(float ExtendedDistance, uint32_t zoom);
    void   clearBuffer();
    void   clearItemsExceptMap();
    void   clearTrackerItems();
    bool   isPointinPolygon(QPointF testPoint, QVector<QPointF> polygon);

    //data
    //map
    dvec2   m_MapCenter_LonLat;
    float   m_MapRange;
    int     m_zoom;
    //radar
    RadarParamInfoStruct m_RadarParam;
    //resolution and region
    float m_resolutionintheZoom;
    float m_scale;
    dvec2 m_Region;

    //data struct
    QMap<int, Tracker*> Track_Path;
    QMap<int, QVector<ItemAttribute_t*>> Track_Data;
    QVector<ItemAttribute_t> point_Data;
    QList<QGraphicsEllipseItem*> point_list;
    safe_semaphore mutex_path;
    safe_semaphore mutex_data;
    safe_semaphore mutex_point;
    QColor colors[10];

private slots:
    void AreaStlectionStarts(QPointF pos, AreaType type);
    void AreaStlecting(QPointF pos, AreaType type);
    void AreaSelectionEnds(AreaType type);

private:
    QVector<QPointF> AreaData;
    QVector<QVector<QPointF>> MonitorAreaVec;
    QVector<QVector<QPointF>> AlertAreaVec;
    QList  <PolygonAreaItem*> AreasItem;
    QList  <PolygonAreaItem*> MonitorItems;
    QList  <PolygonAreaItem*> AlertItems;
};
#endif
